<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use \DB;
use Auth;
use Illuminate\Support\Facades\Request;

class RoleUserController extends CommonController
{

    /**
     * 角色列表。
     *
     * @return Response
     */
    public function getIndex()
    {
        $users = DB::table('admin')->orderBy('sort', 'asc')->paginate(10);

        return view('roleuser.index')->with("users", $users)->with("actions", $this->actions);
    }

    public function getCreate()
    {
        return view('roleuser.create');
    }

    public function postInsert()
    {
        if (!Request::has('nickname')) {
            return AjaxCallbackMessage('用户昵称不能为空', false);
        }
        if (!Request::has('username')) {
            return AjaxCallbackMessage('用户账号不能为空', false);
        } else {
//    		if(!checkMobile(Request::input("username"))){
//    			return 	AjaxCallbackMessage('用户账号格式不正确',false);
//    		}
            $username_count = DB::table('admin')->where('username', Request::input("username"))->count();
            if ($username_count) {
                return AjaxCallbackMessage('用户账号已被注册', false);
            }
        }
        if (Request::has('email')) {
            if (!checkEmail(Request::input("email"))) {
                return AjaxCallbackMessage('用户邮箱格式不正确', false);
            }
            $useremail_count = DB::table('admin')->where('email', Request::input("email"))->count();
            if ($useremail_count) {
                return AjaxCallbackMessage('用户邮箱已被注册', false);
            }
        }
        if (!Request::has('password')) {
            return AjaxCallbackMessage('密码不能为空', false);
        }
        if (!Request::has('password2')) {
            return AjaxCallbackMessage('确认密码不能为空', false);
        }
        if (Request::input('password') !== Request::input('password2')) {
            return AjaxCallbackMessage('两次密码输入不一致', false);
        }
        $data = array();
        $data['nickname'] = Request::input("nickname");
        $data['username'] = Request::input("username");
        $data['password'] = md5(Request::input("password"));
        $data['email'] = Request::input("email");
        $data['created_at'] = time();
        $data['reg_ip'] = getIP();
        $data['status'] = Request::input("status");
        $id = DB::table('admin')->insertGetId($data);

        if ($id) {
            return AjaxCallbackMessage('用户添加成功', true, url('roleuser/index'));
        } else {
            return AjaxCallbackMessage('用户添加失败', false);
        }
    }

    public function getEdit()
    {
        $uid = Request::input("uid");
        $user = DB::table('admin')->where('id', $uid)->first();

        return view('roleuser.edit')->with("user", $user);
    }

    public function postUpdate()
    {
        if (!Request::has('id')) {
            return AjaxCallbackMessage('参数异常', false);
        }
        if (!Request::has('nickname')) {
            return AjaxCallbackMessage('用户昵称不能为空', false);
        }
        if (!Request::has('username')) {
            return AjaxCallbackMessage('用户账号不能为空', false);
        } else {
//    		if(!checkMobile(Request::input("username"))){
//    			return 	AjaxCallbackMessage('用户账号格式不正确',false);
//    		}
            $username_count = DB::table('admin')->where('username', Request::input("username"))->where('id', '<>', Request::input("id"))->count();
            if ($username_count) {
                return AjaxCallbackMessage('用户账号已被注册', false);
            }
        }
        if (Request::has('email')) {
            if (!checkEmail(Request::input("email"))) {
                return AjaxCallbackMessage('用户邮箱格式不正确', false);
            }
            $useremail_count = DB::table('admin')->where('email', Request::input("email"))->where('id', '<>', Request::input("id"))->count();
            if ($useremail_count) {
                return AjaxCallbackMessage('用户邮箱已被注册', false);
            }
        }
        $data = Request::all();
//        dd($data);
        if (!Request::has('password')) {
            return AjaxCallbackMessage('密码不能为空', false);
        }
        if (!Request::has('password2')) {
            return AjaxCallbackMessage('确认密码不能为空', false);
        }
        if (Request::input('password') !== Request::input('password2')) {
            return AjaxCallbackMessage('两次密码输入不一致', false);
        }
        $data = array();
        $data['nickname'] = Request::input("nickname");
        $data['username'] = Request::input("username");
        $data['password'] = md5(Request::input("password"));
        $data['email'] = Request::input("email");
        $data['updated_at'] = time();
        $data['status'] = Request::input("status");
        $id = DB::table('admin')->where('id', Request::input("id"))->update($data);
        if ($id) {
            return AjaxCallbackMessage('用户添加成功', true, url('roleuser/index'));
        } else {
            return AjaxCallbackMessage('用户添加失败', false);
        }
    }

    public function postDelete()
    {
        if (!Request::has('uid')) {
            return AjaxCallbackMessage('数据异常', false);
        }
        $uid = Request::input("uid");
        DB::table('admin')->where('id', '=', $uid)->delete();
        DB::table('adm_role_user')->where('admin_id', '=', $uid)->delete();

        return AjaxCallbackMessage('用户删除成功', true, url('roleuser/index'));
    }
}